package com.example.Thread;


import com.example.service.HandleOnline;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by Studio on 2017/5/18.
 */
public class JudgeThread extends Thread {

   // private static SimpleDateFormat date_format = new SimpleDateFormat("MM_dd_HH");
//    private static SimpleDateFormat date_format1 = new SimpleDateFormat("MM");
//    private static SimpleDateFormat date_format2 = new SimpleDateFormat("dd");
//    private static SimpleDateFormat date_format3 = new SimpleDateFormat("HH");
//
//    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private static final Logger logger = LogManager.getLogger();

    private Jedis jedis;

    private HandleOnline handle;
    private JedisPool jedisPool;


    private  String device_id = "";
    private  String key = "";
    private  String key_hour = "";
    private  String created_hour;



    private Object[] s;

    private int size;
    private long created;


    public JudgeThread(JedisPool jedisPool, HandleOnline handle){
        this.jedisPool = jedisPool;
        this.handle = handle;

    }

    @Override
    public void run(){
        System.out.println("线程启动");
        while (true){
            try {
                jedis = jedisPool.getResource();
                s = jedis.keys("tens/*").toArray();

                size = jedis.keys("tens/*").size();

                if(size > 0){
                    for(int i = 0; i < size;i++){
                        key = s[i].toString();

                        created = Long.parseLong(jedis.get(key));
//                        System.out.println(key);

                        if(((System.currentTimeMillis()/1000)-created) > 60){

                            System.out.println("设备下线："+key+"       :间隔时间:"+((System.currentTimeMillis()/1000)-created));
                            device_id = key.substring(5);

                            key_hour = "hour/"+device_id;

                            System.out.println("设备下线："+key_hour+"       :hour:"+jedis.get(key_hour));
                            created_hour = jedis.get(key_hour);

                            if(created_hour != null){
                                handle.handle_online(device_id,created,Long.parseLong(created_hour));

                                jedis.del(key_hour);
                                jedis.set("hourOffline/"+device_id,created_hour);
                            }else {
                                handle.handle_online(device_id,created);
                            }

                            jedis.del(key);

                            created_hour = "";
                            key_hour = "";
                        }

                        device_id = "";
                        key = "";
                    }
                }

                s.clone();
                jedis.close();
                Thread.sleep(1000);
            } catch (Exception e) {
                logger.debug("JudgeThread:run:exception{}" + e.toString());
                e.printStackTrace();
            }

        }
    }
}
